package com.ruoyi.hospital.hospitalized.service.impl;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hospital.hospitalized.domain.THospitalized;
import com.ruoyi.hospital.hospitalized.mapper.THospitalizedMapper;
import com.ruoyi.hospital.hospitalized.service.ITHospitalizedService;
import com.ruoyi.hospital.order.service.ITOrderService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 住院管理Service业务层处理
 *
 * @author Shawn
 * @date 2024-02-21
 */
@Service
public class THospitalizedServiceImpl implements ITHospitalizedService {

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ITOrderService itOrderService;

    @Autowired
    private THospitalizedMapper tHospitalizedMapper;

    /**
     * 查询住院管理
     *
     * @param id 住院管理主键
     * @return 住院管理
     */
    @Override
    public THospitalized selectTHospitalizedById(Long id) {
        return tHospitalizedMapper.selectTHospitalizedById(id);
    }

    /**
     * 查询住院管理列表
     *
     * @param tHospitalized 住院管理
     * @return 住院管理
     */
    @Override
    public List<THospitalized> selectTHospitalizedList(THospitalized tHospitalized) {
        return tHospitalizedMapper.selectTHospitalizedList(tHospitalized);
    }

    /**
     * 新增住院管理
     *
     * @param tHospitalized 住院管理
     * @return 结果
     */
    @Override
    public int insertTHospitalized(THospitalized tHospitalized) {
        List<THospitalized> tHospitalizedList = tHospitalizedMapper.selectTHospitalizedByPatientId(tHospitalized.getPatientId());
        if (!tHospitalizedList.isEmpty()) {
            throw new ServiceException("患者已经入院");
        }
        SysDictData sysDictData = dictTypeService.findFirstByDataByType("hospital_admission_status");
        if (null != sysDictData) {
            tHospitalized.setAdmissionStatus(sysDictData.getDictValue());
        }
        tHospitalized.setCreatedTime(new Date());
        tHospitalized.setCreatedBy(String.valueOf(ShiroUtils.getUserId()));
        return tHospitalizedMapper.insertTHospitalized(tHospitalized);
    }

    /**
     * 修改住院管理
     *
     * @param tHospitalized 住院管理
     * @return 结果
     */
    @Override
    public int updateTHospitalized(THospitalized tHospitalized) {
        return tHospitalizedMapper.updateTHospitalized(tHospitalized);
    }

    /**
     * 批量删除住院管理
     *
     * @param ids 需要删除的住院管理主键
     * @return 结果
     */
    @Override
    public int deleteTHospitalizedByIds(String ids) {
        return tHospitalizedMapper.deleteTHospitalizedByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除住院管理信息
     *
     * @param id 住院管理主键
     * @return 结果
     */
    @Override
    public int deleteTHospitalizedById(Long id) {
        return tHospitalizedMapper.deleteTHospitalizedById(id);
    }

    @Override
    public int dischargeTHospitalizedByIds(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return 0;
        }
        String[] array = ids.split(",");
        for (String id : array) {
            THospitalized hospitalized = new THospitalized();
            hospitalized.setId(Long.parseLong(id));
            hospitalized.setAdmissionStatus("discharge");
            tHospitalizedMapper.updateTHospitalized(hospitalized);
        }
        return 1;
    }

    @Override
    public int settlement(THospitalized tHospitalized) {
        THospitalized hospitalized = tHospitalizedMapper.selectTHospitalizedById(tHospitalized.getId());
        if (null != hospitalized) {
            hospitalized.setSettlementAmount(tHospitalized.getSettlementAmount());
            itOrderService.insertHospitalizedOrder(hospitalized);
        }
        tHospitalized.setSettlementStatus("1");
        return updateTHospitalized(tHospitalized);
    }

    @Override
    public Map<String,Object> counthospitalized(THospitalized tHospitalized) {
        return tHospitalizedMapper.counthospitalized(tHospitalized);
    }
}
